AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.val1 = 0
	self.damaged = 0
	self.vent = 0
	self.sounded = 0
	
	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity, { "Vent Container" }) end
	if not (WireAddon == nil) then self.Outputs = Wire_CreateOutputs(self.Entity, { "Out" }) end
	
	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
	end
end

function ENT:TriggerInput(iname, value)
	if (iname == "Vent Container") then
		if (value ~= 0) then
			if ( self.vent == 0 ) then
				self.vent = 1
			end
		else
			if ( self.vent == 1 ) then
				self.vent = 0
			end
		end
	end
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
		self.Entity:EmitSound( "PhysicsCannister.ThrusterLoop" )
	end
end

function ENT:Repair()
	self.Entity:SetColor(255, 255, 255, 255)
	self.health = self.max_health
	self.damaged = 0
	self.Entity:StopSound( "PhysicsCannister.ThrusterLoop" )
end

function ENT:Destruct()
	local Effect = EffectData()
		Effect:SetOrigin(self.Entity:GetPos())
		Effect:SetScale(1)
		Effect:SetMagnitude(25)
		util.Effect("Explosion", Effect, true, true)

		self.Entity:Remove()
end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
	self.Entity:StopSound( "PhysicsCannister.ThrusterLoop" )
end



function ENT:Output()
	return 1
end

function ENT:ShowOutput(value)
	self:SetOverlayText( "Coolant\n(" .. value .. ")" )
	return value
end

function ENT:Think()
	self:ShowOutput(self.val1)
	if ( self.timer > CurTime() ) then return end
	local coolant = RD_GetResourceAmount(self, "coolant")
	
	self.val1 = coolant
	
	if self.vent == 1 then --MAYDAY! VENT THE CONTAINER! (lol)
		RD_ConsumeResource(self, "coolant", self.val1)
		if self.sounded == 0 then
			self.Entity:EmitSound( "PhysicsCannister.ThrusterLoop" )
			self.Entity:EmitSound( "ambient.steam01" )
			self.sounded = 1
		end
	elseif (self.sounded == 1 and self.vent == 0) then
		self.Entity:StopSound( "PhysicsCannister.ThrusterLoop" )
		self.Entity:StopSound( "ambient.steam01" )
		self.sounded = 0
	end
	
	if ((self.damaged == 1) and (coolant > 0)) then
		if (coolant >= 100) then
			RD_ConsumeResource(self, "coolant", 100)
		else
			RD_ConsumeResource(self, "coolant", coolant)
			self.Entity:StopSound( "PhysicsCannister.ThrusterLoop" )
		end
	end

	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", coolant) end
	self.timer = CurTime() +  1
end

function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
